package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前用户的地址簿
     *
     * @return
     */
    @ApiOperation("查询当前用户的地址簿")
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        log.info("查询当前用户的地址簿");
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        log.info("新增地址:{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据 id 查询地址信息 - 页面回显
     *
     * @param id
     * @return
     */
    @ApiOperation("根据 id 查询地址信息")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        log.info("根据 id 查询地址信息 : {}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据 id 修改地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("根据 id 修改地址")
    @PutMapping
    public Result editInfo(@RequestBody AddressBook addressBook) {
        log.info("修改地址 : {}", addressBook);
        addressBookService.editInfo(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @ApiOperation("删除地址")
    @DeleteMapping
    public Result deleteById(Long id) {
        log.info("删除地址: {}", id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址 : {}", addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        log.info("查询默认地址");
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

}
